package unit6.part6;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ComparableTest {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("A", 100, 22));
        list.add(new Student("B", 80, 19));
        list.add(new Student("C", 80, 18));
        Collections.sort(list);// 从小到大
        Collections.reverse(list);// 反转
        System.out.println(list);
    }
}

class Student implements Comparable<Student> {
    String name;
    int score;
    int age;

    public Student(String name, int score, int age) {
        this.name = name;
        this.score = score;
        this.age = age;
    }

    public int compareTo(Student o) {// 大于0为this>o，小于0为this<o，等于0为this=o
        if (score == o.score) {
            return o.age - age;
        } else {
            return score - o.score;
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
